﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.DataVisualization.Charting;
using System.Web;

namespace Wicresoft.SMS.Common
{
    public static class ReportHelper
    {
        private static int i = 1;
        public static string GetImageRoute(List<ModelReport> Items, System.Web.UI.DataVisualization.Charting.SeriesChartType ChartType, DateTime StartTime, DateTime EndTime, string DepartmentName)
        {
            System.Web.UI.DataVisualization.Charting.Chart chart = new System.Web.UI.DataVisualization.Charting.Chart();
            chart.Width = 600;
            chart.Height = 300;
            chart.RenderType = RenderType.ImageTag;
            chart.BackColor = System.Drawing.Color.Thistle;
            chart.Palette = ChartColorPalette.Berry;
            chart.ChartAreas.Add("Series");
            chart.Series.Add("Count");
            chart.Series["Count"].ChartType = ChartType;//设定类型
            chart.Series["Count"].IsValueShownAsLabel = true;//显示数值
            chart.Series["Count"].BorderWidth = 2;//设定宽度

            foreach (var item in Items)
            {
                chart.Series["Count"].Points.AddXY(item.Name, item.Count);
            }

            chart.Legends.Add("Count");
            
            string title = String.Empty;
            if (ChartType == System.Web.UI.DataVisualization.Charting.SeriesChartType.Line)
            {
                title = "公司短信发送量:(" + StartTime.ToString("yyyy-MM-dd") + "~" + EndTime.ToString("yyyy-MM-dd") + ")";
            }
            else if (DepartmentName == "GroupReport")
                title = "部门短信发送量:(" + StartTime.ToString("yyyy-MM-dd") + "~" + EndTime.ToString("yyyy-MM-dd") + ")";
            else if (ChartType == System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie && DepartmentName != "0")
                title = DepartmentName + "部门个人短信发送量:(" + StartTime.ToString("yyyy-MM-dd") + "~" + EndTime.ToString("yyyy-MM-dd") + ")";
            else if (ChartType == System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie && DepartmentName == "0")
                title = "个人短信发送量:(" + StartTime.ToString("yyyy-MM-dd") + "~" + EndTime.ToString("yyyy-MM-dd") + ")";
            Title t = new Title(title, Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            chart.Titles.Add(t);
            string name = i.ToString() + ".png";
            var route = HttpContext.Current.Server.MapPath("./") + @"..\..\Content\Image\Report\" + name;
            try
            {
                chart.SaveImage(route, ChartImageFormat.Png);
            }
            catch
            {
                route = HttpContext.Current.Server.MapPath("./") + @"..\Content\Image\Report\" + name;
                chart.SaveImage(route, ChartImageFormat.Png);
            }
            i = i < 1000 ? i+1 : 1;//防止图片文件过多
            return name;
        }
    }

}
